<?php
class Admin_RouteController extends Zend_Controller_Action
{
    public function init()
    {

    }
	
	public function listAction()
	{
		// create search form
		$searchForm = new Admin_Form_RouteSearch();
		$searchForm->setAction('/admin/route/list')->setMethod('post');
		$this->view->searchForm = $searchForm;
		
		// get params from request
		$sorter = $this->_request->getParam('sorter', null);
		$filter = $this->_request->getParam('filter', null);
		$filterText = $this->_request->getParam('filterText', null);
		
		// populate them to form
		$searchForm->getElement('sorter')->setValue($sorter);
		$searchForm->getElement('filter')->setValue($filter);
		$searchForm->getElement('filterText')->setValue($filterText);
			
		// list route
		$routeModel = new Admin_Model_Route();		
		$routeAdapter = $routeModel->getRoutePaginatorAdapter($filter, $filterText, $sorter);		
		
		if($routeAdapter) {
			$paginator = new Zend_Paginator($routeAdapter);
			$page = $this->_request->getParam('page', 1);
			$paginator->setItemCountPerPage(5);
			$paginator->setCurrentPageNumber($page);	
			$paginator->setPageRange(8);
			$this->view->paginator = $paginator;
		}
	}
	
	public function createAction()
	{
		$routeForm = new Admin_Form_Route();
		$routeForm->setAction('/admin/route/create')->setMethod('post');
		
		// add from and to elements
		$cityModel = new Admin_Model_City();		
		$cities = $cityModel->getCityNameCityNameMap();			
		$routeForm->addFromToCityElement($cities);
						
		if($this->_request->isPost()) {
			// get params from $_POST
			$from = $this->_request->getParam('from');
			$to = $this->_request->getParam('to');
									
			if($routeForm->isValid($_POST))
			{			
				$data = $routeForm->getValues();				
				$routeModel = new Admin_Model_Route();

				try {
					$routeModel->createRoute(
						$from,
						$to,
						$data['boardingPoint'],
						$data['droppingPoint']
					);					
				} catch(Exception $e) {
					return TBB_Utility_Redirector::redirect($e->getMessage(), 'admin', 'error', 'fatal-error');					
				}
				return $this->_redirect('/admin/route/list');
			}
		}
		$this->view->form = $routeForm;
	}
	
	public function updateAction()
	{
		$routeForm = new Admin_Form_Route();
		$routeForm->setAction('/admin/route/update')->setMethod('post');	

		// get city array
		$cityModel = new Admin_Model_City();		
		$cities = $cityModel->getCityNameCityNameMap();	
		$routeForm->addFromToCityElement($cities);
			
		if($this->_request->isPost()) {	
			// get params from request
			$id = $this->_request->getParam('routeID');			
			$from = $this->_request->getParam('from');
			$to = $this->_request->getParam('to');			
					 
			if($routeForm->isValid($_POST)) {
				$data = $routeForm->getValues(); 
				$routeModel = new Admin_Model_Route();				
				$routeModel->updateRoute(
					$data['routeID'],
					$from,
					$to,
					$data['boardingPoint'],
					$data['droppingPoint']
				);
				$this->_redirect('/admin/route/list');
			}
		} else {
			// get route ID from request
			$id = $this->_request->getParam('id');
		}
		
		// populate form
		$routeModel = new Admin_Model_Route();
		$routeRow = $routeModel->find($id)->current();
		if($routeRow) {			
			$routeForm->getElement('routeID')->setValue($routeRow->route_id);
			$routeForm->setFromToCityValue($routeRow->from_city, $routeRow->to_city);			
			$routeForm->getElement('boardingPoint')->setValue($routeRow->boarding_point);
			$routeForm->getElement('droppingPoint')->setValue($routeRow->dropping_point);
		}	
		
		$this->view->form = $routeForm;
	}
	
	public function deleteAction()
	{
		$id = $this->_request->getParam('id');
		$routeModel = new Admin_Model_Route();
		try {
			$routeModel->deleteRoute($id);			
		} catch (Exception $e) {
			return TBB_Utility_Redirector::redirect($e->getMessage(), 'admin', 'error', 'fatal-error');
		}
		return $this->_redirect('/admin/route/list');	
	}
	

}